define(['zepto', 'JST', 'ctx', 'utils', 'wxConfig', 'wxShare', 'iscroll', 'lazyLoad', 'suiExtend', 'sui'], function($, JST, ctx, utils, wxConfig, wxShare ,IScroll, lazyLoad) {
	return {

		init: function() {
			var that = this

			this.model = {
				'page': 1,
			}

			this.onMenuClick();              //right menus

			wxConfig.config(
        'onMenuShareTimeline',
        'onMenuShareAppMessage',
        'onMenuShareQQ',
        'onMenuShareWeibo',
        'onMenuShareQZone'
      );
			wxShare.openShare("活动新闻", "人马君最新活动最新动态尽在掌握");

			this.compileArticlePage();	// compile article page

			this.model.pageScroll = new IScroll('.content', {
				click: that.iScrollClick()
			});

			this.model.pageScroll.on('scrollEnd', function(e){
	        	
	        	if (this.maxScrollY < 0 && this.startY + this.distY <= this.maxScrollY - 50) {

	      			if(!that.model.endLoad) that.pageLoad()
	    	    }
	        });

	        lazyLoad.piclazyload();

		},

		iScrollClick :function(){

			if (/iPhone|iPad|iPod|Macintosh/i.test(navigator.userAgent)) return false;
			if (/Chrome/i.test(navigator.userAgent)) return (/Android/i.test(navigator.userAgent));
			if (/Silk/i.test(navigator.userAgent)) return false;
			if (/Android/i.test(navigator.userAgent)) {
	  		var s=navigator.userAgent.substr(navigator.userAgent.indexOf('Android')+8,3);
	  		return parseFloat(s[0]+s[3]) < 44 ? false : true
    			}
		},

		onMenuClick: function(){

			$(document).on("click", ".header-menu", function() {
			  $.openPanel("#panel-right-menu");
			});
		},
		getUrlVars: function(){
		    var vars = [], hash;

		    if(window.location.href.indexOf('?') == -1) {
		    	return null;
		    }else {
		    	var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
			    for(var i = 0; i < hashes.length; i++)
			    {
			    	hash = hashes[i].split('=');
			    
			    	vars[hash[0]] = hash[1];
			    }
			    return vars;
		    }
		    
	  	},

	  	getActivity: function() {

			var that = this;

			var defer = $.Deferred(),
	  			url   = ctx.host + 'activities?page=' + that.model.page

			//show request indicator
			$.showIndicator();

			//request  for rounds data
			$.ajax({
				url: 	url,
				type:  'GET',
				cache:  false
			})
			.done(function(response) {
				$.hideIndicator();
				utils.processResult(response, function(result){
					defer.resolve(result.data)
				},function(result){
					$.toast(result.message || '获取文章模块失败')
				})
			})
			
			return defer.promise()

		},
		compileArticlePage: function(page) {
			var that = this,
          		deferArticle = that.getActivity();

       		$.when(deferArticle).done(function(activityData){

      			//add data to article modal & page template
      			$article =  JST.ActivityartTpl({
      				'activities'  : activityData.activities,
      				'domain'	  : ctx.domain,
      				'imgsurl'	  : ctx.cdn
      			})

      			$("#activity-article").empty().append($article)
      			that.model.pageScroll.refresh()
      			that.model.page = that.model.page + 1;
      		});
		},

		pageLoad: function() {
			var that = this,
          		deferArticle = that.getActivity();

          	$.when(deferArticle).done(function(activityData){

      			//add data to article model & page template
      			$article =  JST.ActivityartTpl({
      				'activities'  : activityData.activities,
      				'domain'	  : ctx.domain,
      				'imgsurl'	  : ctx.cdn
      			})

      			if(!activityData.page.nextPage) {
      				that.model.endLoad = true;
      				$(".pull-color").html("已无更多文章")
      			}

      			$("#activity-article").append($article)
      			that.model.pageScroll.refresh()
      			that.model.page = that.model.page + 1;
      		})
		}
	
	}
})